#include "switchcontrol.h"
#include <QTime>
SwitchControl::SwitchControl()
{
    this->moveToThread(&thread);
    thread.start();
    StartSiganlInit();
    QThread::msleep(1000);
    emit Signal_ToNewDevice();
    modbus->ConnectSignalSlots();
    // monitor.StartThread();
    SignalSlotsInit();
}

SwitchControl::~SwitchControl()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    timer->stop();
    thread.quit();
    thread.wait();
    qDebug()<<__FUNCTION__<<__LINE__;
}

void SwitchControl::StartSiganlInit()
{
    connect(this,SIGNAL(Signal_ToNewDevice()),this,SLOT(Slots_ToNewDevice()),Qt::BlockingQueuedConnection);
}

void SwitchControl::SignalSlotsInit()
{
    connect(this,SIGNAL(Signal_ToOpenDevice(QString)),this,SLOT(Slots_ToOpenDevice(QString)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToSetOnoff(int,bool)),this,SLOT(Slots_ToSetOnoff(int,bool)));
    connect(timer,SIGNAL(timeout()),this,SLOT(Slots_TimerTimeout()));
    connect(this,SIGNAL(Signal_ToStartTimer()),this,SLOT(Slots_ToStartTimer()));
    connect(this,SIGNAL(Signal_ToStopTimer()),this,SLOT(Slots_ToStopTimer()));

    connect(this,SIGNAL(Signal_ToGetInputValue(bool*,unsigned char *)),this,SLOT(Slots_ToGetInputValue(bool*,unsigned char *)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToGetControlValue(bool*,unsigned char*)),this,SLOT(Slots_ToGetControlValue(bool *,unsigned char *)),Qt::BlockingQueuedConnection);
}

void SwitchControl::OpenDevice(QString PortName)
{
    emit Signal_ToOpenDevice(PortName);
}

void SwitchControl::SetSwitchOnOff(int RlayAdress, bool onoff)
{
    emit Signal_ToSetOnoff(RlayAdress,onoff);
}

void SwitchControl::StartTimer()
{
    emit Signal_ToStartTimer();
}

void SwitchControl::StopTimer()
{
    emit Signal_ToStopTimer();
}

SwitchInput SwitchControl::GetAllInputValue()
{
    SwitchInput switchInput;
    switchInput.success=false;

    unsigned char recv=0;
    bool ret=false;
    emit Signal_ToGetInputValue(&ret,&recv);
    if(ret)
    {
        qDebug()<<"~";
        switchInput.success=true;
        if((recv&0x01)==0x01)
            switchInput.onOff[0]=true;
        if((recv&0x02)==0x02)
            switchInput.onOff[1]=true;
        if((recv&0x04)==0x04)
            switchInput.onOff[2]=true;
        if((recv&0x08)==0x08)
            switchInput.onOff[3]=true;
    }
    return switchInput;
}

SwitchControlValue SwitchControl::GetAllControlValue()
{
    SwitchControlValue switchControlValue;
    switchControlValue.sucess=false;
    unsigned char recv=0;
    bool ret=false;
    emit Signal_ToGetControlValue(&ret,&recv);
    if(ret)
    {
        switchControlValue.sucess=true;
        if((recv&0x01)==0x01)
            switchControlValue.onOff[0]=true;
        if((recv&0x02)==0x02)
            switchControlValue.onOff[1]=true;
        if((recv&0x04)==0x04)
            switchControlValue.onOff[2]=true;
        if((recv&0x08)==0x08)
            switchControlValue.onOff[3]=true;
    }
    return switchControlValue;
}

void SwitchControl::Slots_ToOpenDevice(QString portName)
{
    qDebug()<<__FUNCTION__;
    qDebug()<<"打开串口的线程ID"<<QThread::currentThread();
    modbus->port.setPortName(portName);
    modbus->port.open(QSerialPort::ReadWrite);
}

void SwitchControl::Slots_ToNewDevice()
{
    qDebug()<<__FUNCTION__;
    qDebug()<<"new 对象的线程ID"<<QThread::currentThread();
    modbus=new Modbus();
    modbus->port.setBaudRate(9600);
    modbus->port.setParity(QSerialPort::NoParity);
    modbus->port.setDataBits(QSerialPort::Data8);
    modbus->port.setStopBits(QSerialPort::OneStop);
    timer=new QTimer();
}

void SwitchControl::Slots_ToSetOnoff(int RlayAdress, bool onoff)
{
    qDebug()<<__FUNCTION__;

    ushort address=RlayAdress;
    ushort value=0x0000;
    if(onoff ==false)
        value=0;
    else if(onoff==true)
        value=0xff00;
    modbus->SendF5(0xfe,address,value);
    qDebug()<<__FUNCTION__<<__LINE__;
}

void SwitchControl::Slots_ToStartTimer()
{
    timer->start(5);
}

void SwitchControl::Slots_ToStopTimer()
{
    timer->stop();
}

void SwitchControl::Slots_TimerTimeout()
{
    isRunning=true;
    if(!jumpQuerry)
    {
      //  qDebug()<<__FUNCTION__<<__LINE__;
        unsigned char recv=0;
        modbus->SendFc2(0xfe,0,4,&recv);
        locker.lock();
        if((recv&0x01)==0x01)
        {
            if(left.count()<=0)
            {
                left.append(1);
                emit Signal_ToShowBumb(1);
            }
        }

        if((recv&0x02)==0x02)
        {
            if(right.count()<=0)
            {
                right.append(1);
                emit Signal_ToShowBumb(2);
            }
        }
        locker.unlock();

        modbus->SendFc1(0xfe,0,4,&recv);
        if((recv&0x01)==0x01)
            controlValue.onOff[0]=true;
        else
        {
            controlValue.onOff[0]=false;
        }
        if((recv&0x02)==0x02)
            controlValue.onOff[1]=true;
        else
            controlValue.onOff[1]=false;
        if((recv&0x04)==0x04)
            controlValue.onOff[2]=true;
        else
            controlValue.onOff[2]=false;
        if((recv&0x08)==0x08)
            controlValue.onOff[3]=true;
        else
            controlValue.onOff[3]=false;
    }
    //qDebug()<<QTime::currentTime();
    //qDebug()<<recv;
    isRunning=false;
    //qDebug()<<__FUNCTION__<<__LINE__;
}

void SwitchControl::Slots_ToGetInputValue(bool*ret,unsigned char *recv)
{
    *ret=modbus->SendFc2(0xfe,0,4,recv);
}

void SwitchControl::Slots_ToGetControlValue(bool *ret, unsigned char *recv)
{
    *ret=modbus->SendFc1(0xfe,0,4,recv);
}


Monitor::Monitor()
{
    this->moveToThread(&thread);
    thread.start();
    connect(this,SIGNAL(StartMonitor()),this,SLOT(MonitorData()));
}

void Monitor::StartThread()
{
    bRun=true;
    emit StartMonitor();
}

void Monitor::MonitorData()
{
    while (bRun)
    {
        qDebug()<<QThread::currentThread();
        unsigned char recv=0;
        modbus->SendFc2(0xfe,0,4,&recv);
        if((recv&0x01)==0x01)
        {
            if(left.count()<=0)
            {
                left.append(1);
            }
        }

        if((recv&0x02)==0x02)
        {
            if(right.count()<=0)
            {
                right.append(1);
            }
        }
        QThread::msleep(5);
    }
}
